Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
866e7a0
1
Parent(s):
2aae303
working
Browse files- app.py +17 -13
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -2,9 +2,11 @@ import spaces
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
import subprocess
|
| 5 |
-
import
|
| 6 |
from gradio import State
|
| 7 |
import asyncio
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Function to start the ochat server
|
| 10 |
@spaces.GPU
|
|
@@ -28,7 +30,7 @@ def start_ochat_server():
|
|
| 28 |
start_ochat_server()
|
| 29 |
|
| 30 |
# Function to send a message to the ochat server and get a response
|
| 31 |
-
def chat_with_ochat(message):
|
| 32 |
base_url = "http://localhost:18888"
|
| 33 |
chat_url = f"{base_url}/v1/chat/completions"
|
| 34 |
headers = {"Content-Type": "application/json"}
|
|
@@ -37,14 +39,16 @@ def chat_with_ochat(message):
|
|
| 37 |
"messages": [{"role": "user", "content": message}]
|
| 38 |
}
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Create a Gradio Blocks interface with session state
|
| 50 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
@@ -60,14 +64,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 60 |
|
| 61 |
history = State([]) # Session state for chat history
|
| 62 |
|
| 63 |
-
def user(message, history):
|
| 64 |
return "", history + [[message, None]]
|
| 65 |
|
| 66 |
|
| 67 |
-
def bot(history):
|
| 68 |
if history and history[-1] and history[-1][0]:
|
| 69 |
user_message = history[-1][0]
|
| 70 |
-
bot_response = chat_with_ochat(user_message)
|
| 71 |
history[-1][1] = bot_response # Update the last entry with the bot's response
|
| 72 |
return history
|
| 73 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
import subprocess
|
| 5 |
+
import aiohttp
|
| 6 |
from gradio import State
|
| 7 |
import asyncio
|
| 8 |
+
import json
|
| 9 |
+
import asyncio
|
| 10 |
|
| 11 |
# Function to start the ochat server
|
| 12 |
@spaces.GPU
|
|
|
|
| 30 |
start_ochat_server()
|
| 31 |
|
| 32 |
# Function to send a message to the ochat server and get a response
|
| 33 |
+
async def chat_with_ochat(message):
|
| 34 |
base_url = "http://localhost:18888"
|
| 35 |
chat_url = f"{base_url}/v1/chat/completions"
|
| 36 |
headers = {"Content-Type": "application/json"}
|
|
|
|
| 39 |
"messages": [{"role": "user", "content": message}]
|
| 40 |
}
|
| 41 |
|
| 42 |
+
async with aiohttp.ClientSession() as session:
|
| 43 |
+
try:
|
| 44 |
+
async with session.post(chat_url, headers=headers, json=data) as response:
|
| 45 |
+
if response.status == 200:
|
| 46 |
+
response_data = await response.json()
|
| 47 |
+
return response_data['choices'][0]['message']['content']
|
| 48 |
+
else:
|
| 49 |
+
return f"Error: Server responded with status code {response.status}"
|
| 50 |
+
except aiohttp.ClientError as e:
|
| 51 |
+
return f"Error: {e}"
|
| 52 |
|
| 53 |
# Create a Gradio Blocks interface with session state
|
| 54 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
|
|
| 64 |
|
| 65 |
history = State([]) # Session state for chat history
|
| 66 |
|
| 67 |
+
async def user(message, history):
|
| 68 |
return "", history + [[message, None]]
|
| 69 |
|
| 70 |
|
| 71 |
+
async def bot(history):
|
| 72 |
if history and history[-1] and history[-1][0]:
|
| 73 |
user_message = history[-1][0]
|
| 74 |
+
bot_response = await chat_with_ochat(user_message)
|
| 75 |
history[-1][1] = bot_response # Update the last entry with the bot's response
|
| 76 |
return history
|
| 77 |
|
requirements.txt
CHANGED
|
@@ -5,3 +5,4 @@ datasets
|
|
| 5 |
accelerate
|
| 6 |
numpy
|
| 7 |
ochat
|
|
|
|
|
|
| 5 |
accelerate
|
| 6 |
numpy
|
| 7 |
ochat
|
| 8 |
+
aiohttp
|