Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,64 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
|
| 9 |
-
huggingface_token = os.environ.get("huggingface_token")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@app.post("/chat")
|
| 17 |
-
async def chat(message:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
return reply
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
import os
|
| 5 |
+
import gradio as gr
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=os.getenv("huggingface_token"))
|
|
|
|
| 10 |
|
| 11 |
+
class Message(BaseModel):
|
| 12 |
+
input: str
|
| 13 |
+
history: list
|
| 14 |
|
| 15 |
+
@app.get("/")
|
| 16 |
+
async def home():
|
| 17 |
+
return {"message": "Welcome to the chatbot API!"}
|
| 18 |
|
| 19 |
@app.post("/chat")
|
| 20 |
+
async def chat(message: Message):
|
| 21 |
+
try:
|
| 22 |
+
input_message = message.input
|
| 23 |
+
history = message.history
|
| 24 |
+
|
| 25 |
+
# Call the chatbot function
|
| 26 |
+
response = chatbot(input_message, history)
|
| 27 |
+
|
| 28 |
+
return {"response": response}
|
| 29 |
+
|
| 30 |
+
except Exception as e:
|
| 31 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 32 |
|
| 33 |
+
def chatbot(input, history):
|
| 34 |
+
try:
|
| 35 |
+
# Call your chatbot function here
|
| 36 |
+
message = [{"role": "user", "content": input}]
|
| 37 |
+
history = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 38 |
+
messages = history + message
|
| 39 |
+
|
| 40 |
+
output = client.chat_completion(
|
| 41 |
+
messages=messages,
|
| 42 |
+
max_tokens=256,
|
| 43 |
+
temperature=0.7
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
history = history + [{"role": "assistant", "content": output.choices[0].message.content}]
|
| 47 |
+
|
| 48 |
+
return output.choices[0].message.content
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
raise Exception(str(e))
|
| 52 |
|
| 53 |
+
# Define the Gradio chat interface
|
| 54 |
+
def gradio_chat(input, history):
|
| 55 |
+
response = chatbot(input, history)
|
| 56 |
+
return response
|
| 57 |
|
| 58 |
+
# Define Gradio inputs and outputs
|
| 59 |
+
inputs = [gr.Textbox(lines=5, label="Input"), gr.Textbox(lines=5, label="History")]
|
| 60 |
+
output = gr.Textbox(label="Response")
|
| 61 |
|
| 62 |
+
# Create Gradio interface
|
| 63 |
+
gr.Interface(fn=gradio_chat, inputs=inputs, outputs=output, title="Chatbot").launch()
|
| 64 |
|
|
|