Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import requests
|
| 4 |
-
from typing import
|
| 5 |
|
| 6 |
# ____
|
| 7 |
# / __ \
|
|
@@ -16,27 +16,12 @@ from typing import Iterator, List, Dict
|
|
| 16 |
|
| 17 |
API_URL = "https://app.oxyapi.uk/free/v1/chat/completions"
|
| 18 |
|
| 19 |
-
def create_stream_response(response: requests.Response) -> Iterator[str]:
|
| 20 |
-
for line in response.iter_lines():
|
| 21 |
-
if not line or line.decode('utf-8').startswith("data: [DONE]"):
|
| 22 |
-
continue
|
| 23 |
-
try:
|
| 24 |
-
line_text = line.decode('utf-8')
|
| 25 |
-
if line_text.startswith("data: "):
|
| 26 |
-
json_response = json.loads(line_text[6:])
|
| 27 |
-
if 'choices' in json_response:
|
| 28 |
-
delta = json_response['choices'][0].get('delta', {})
|
| 29 |
-
if 'content' in delta:
|
| 30 |
-
yield delta['content']
|
| 31 |
-
except json.JSONDecodeError:
|
| 32 |
-
continue
|
| 33 |
-
|
| 34 |
def predict(
|
| 35 |
message: str,
|
| 36 |
chat_history: List[Dict[str, str]],
|
| 37 |
temperature: float,
|
| 38 |
top_p: float,
|
| 39 |
-
) ->
|
| 40 |
messages = [
|
| 41 |
{
|
| 42 |
"role": "system",
|
|
@@ -47,8 +32,7 @@ def predict(
|
|
| 47 |
payload = {
|
| 48 |
"messages": messages,
|
| 49 |
"temperature": temperature,
|
| 50 |
-
"top_p": top_p
|
| 51 |
-
"stream": True
|
| 52 |
}
|
| 53 |
|
| 54 |
headers = {
|
|
@@ -56,25 +40,27 @@ def predict(
|
|
| 56 |
"Authorization": "Bearer oxy-1-small-gradio"
|
| 57 |
}
|
| 58 |
|
| 59 |
-
chat_history = chat_history + [{"role": "user", "content": message}
|
| 60 |
try:
|
| 61 |
response = requests.post(
|
| 62 |
API_URL,
|
| 63 |
headers=headers,
|
| 64 |
-
json=payload
|
| 65 |
-
stream=True
|
| 66 |
)
|
| 67 |
response.raise_for_status()
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
assistant_content
|
| 72 |
-
chat_history
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
-
chat_history
|
| 77 |
-
|
| 78 |
|
| 79 |
css = """
|
| 80 |
.gradio-container {
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import requests
|
| 4 |
+
from typing import List, Dict
|
| 5 |
|
| 6 |
# ____
|
| 7 |
# / __ \
|
|
|
|
| 16 |
|
| 17 |
API_URL = "https://app.oxyapi.uk/free/v1/chat/completions"
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def predict(
|
| 20 |
message: str,
|
| 21 |
chat_history: List[Dict[str, str]],
|
| 22 |
temperature: float,
|
| 23 |
top_p: float,
|
| 24 |
+
) -> List[Dict[str, str]]:
|
| 25 |
messages = [
|
| 26 |
{
|
| 27 |
"role": "system",
|
|
|
|
| 32 |
payload = {
|
| 33 |
"messages": messages,
|
| 34 |
"temperature": temperature,
|
| 35 |
+
"top_p": top_p
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
headers = {
|
|
|
|
| 40 |
"Authorization": "Bearer oxy-1-small-gradio"
|
| 41 |
}
|
| 42 |
|
| 43 |
+
chat_history = chat_history + [{"role": "user", "content": message}]
|
| 44 |
try:
|
| 45 |
response = requests.post(
|
| 46 |
API_URL,
|
| 47 |
headers=headers,
|
| 48 |
+
json=payload
|
|
|
|
| 49 |
)
|
| 50 |
response.raise_for_status()
|
| 51 |
|
| 52 |
+
json_response = response.json()
|
| 53 |
+
if 'choices' in json_response and len(json_response['choices']) > 0:
|
| 54 |
+
assistant_content = json_response['choices'][0]['message']['content']
|
| 55 |
+
chat_history.append({"role": "assistant", "content": assistant_content})
|
| 56 |
+
else:
|
| 57 |
+
chat_history.append({"role": "assistant", "content": "Error: No response from assistant."})
|
| 58 |
+
|
| 59 |
+
return chat_history
|
| 60 |
+
|
| 61 |
except Exception as e:
|
| 62 |
+
chat_history.append({"role": "assistant", "content": f"Error: {str(e)}"})
|
| 63 |
+
return chat_history
|
| 64 |
|
| 65 |
css = """
|
| 66 |
.gradio-container {
|