Spaces:
Runtime error
Runtime error
File size: 1,691 Bytes
7f45f25 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import gradio as gr
import openai
from openai import AzureOpenAI
from openai import OpenAIError, RateLimitError
from dotenv import load_dotenv
import os
load_dotenv()
AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
AZURE_OPENAI_KEY = os.getenv("AZURE_OPENAI_KEY")
AZURE_API_VERSION = os.getenv("AZURE_API_VERSION")
client = AzureOpenAI(
azure_endpoint=AZURE_OPENAI_ENDPOINT,
api_key=AZURE_OPENAI_KEY,
api_version=AZURE_API_VERSION
)
from openai import OpenAI,AsyncAzureOpenAI
from client import gradio_client
import gradio as gr
import logging
logging.basicConfig(level=logging.INFO)
import time
import asyncio
def get_streaming_response(query):
try:
messages, urls = gradio_client.predict(
query=query,
api_name="/predict")
logging.info("Starting streaming response...")
response = client.chat.completions.create(
messages=messages,
model="urdu-llama",
temperature=0.5,
stream=True
)
output = ''
for chunk in response:
if chunk.choices:
token = chunk.choices[0].delta.content
if token:
output += token
yield output,urls
except Exception as e:
yield f"Error: {str(e)}"
iface = gr.Interface(
fn=get_streaming_response,
inputs=gr.Textbox(placeholder="Ask me anything...", show_label=False),
outputs=[
gr.Markdown(label="AI Response"),
# gr.JSON(label="Related Questions to Explore:"),
gr.JSON(label="URLs")
],
live=False
)
iface.launch() |