Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,94 +1,13 @@
|
|
| 1 |
import uvicorn
|
| 2 |
import threading
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
-
from groq import Groq
|
| 7 |
-
import gradio as gr
|
| 8 |
-
import requests
|
| 9 |
|
| 10 |
-
# Initialize the FastAPI app
|
| 11 |
-
app = FastAPI()
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
if not groq_api_key:
|
| 16 |
-
raise ValueError("GROQ_API_KEY environment variable is not set")
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
System_msg = '''-act as an experienced blockchain developer, working for 15 years.
|
| 23 |
-
-help me understand some concepts, assume I am a complete beginner.
|
| 24 |
-
-If the user asks anything not related to blockchain, just say you don't know about it.'''
|
| 25 |
-
|
| 26 |
-
# Request model for FastAPI
|
| 27 |
-
class ChatRequest(BaseModel):
|
| 28 |
-
message: str
|
| 29 |
-
history: list
|
| 30 |
-
|
| 31 |
-
# FastAPI chat endpoint
|
| 32 |
-
@app.post("/chat")
|
| 33 |
-
def chat(request: ChatRequest):
|
| 34 |
-
message = request.message
|
| 35 |
-
history = request.history
|
| 36 |
-
|
| 37 |
-
# Create the history_list to send to the Groq API
|
| 38 |
-
history_list = [{"role": "system", "content": System_msg}]
|
| 39 |
-
for human, ai in history:
|
| 40 |
-
history_list.append({"role": "user", "content": human})
|
| 41 |
-
history_list.append({"role": "assistant", "content": ai})
|
| 42 |
-
|
| 43 |
-
# Append the new user message to the history
|
| 44 |
-
history_list.append({"role": "user", "content": message})
|
| 45 |
-
|
| 46 |
-
# Try to get the response from the LLaMA API (Groq)
|
| 47 |
-
try:
|
| 48 |
-
response = client.chat.completions.create(
|
| 49 |
-
model="llama-3.1-70b-versatile", # Ensure the correct model name
|
| 50 |
-
messages=history_list,
|
| 51 |
-
temperature=1.0,
|
| 52 |
-
max_tokens=4000,
|
| 53 |
-
stream=False # Use streaming for real-time responses
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
final_message = response.choices[0].message.content
|
| 57 |
-
|
| 58 |
-
# Return the final AI-generated message
|
| 59 |
-
return {"response": final_message}
|
| 60 |
-
|
| 61 |
-
except Exception as e:
|
| 62 |
-
return {"response": f"Error: {str(e)}"}
|
| 63 |
-
|
| 64 |
-
# Gradio Interface
|
| 65 |
-
def predict(message, history):
|
| 66 |
-
# API request to FastAPI backend
|
| 67 |
-
response = requests.post("http://0.0.0.0:8000/chat", json={"message": message, "history": history})
|
| 68 |
-
|
| 69 |
-
if response.status_code == 200:
|
| 70 |
-
response_data = response.json()
|
| 71 |
-
return response_data['response']
|
| 72 |
-
else:
|
| 73 |
-
return "Error: Unable to connect to the FastAPI backend."
|
| 74 |
-
|
| 75 |
-
demo = gr.ChatInterface(
|
| 76 |
-
predict,
|
| 77 |
-
title="Blockchain Teacher",
|
| 78 |
-
theme=gr.themes.Soft(),
|
| 79 |
-
chatbot=gr.Chatbot(label="Learn about blockchain technology"),
|
| 80 |
-
textbox=gr.Textbox(
|
| 81 |
-
placeholder="Ask me anything about blockchain",
|
| 82 |
-
scale=7,
|
| 83 |
-
max_lines=2,
|
| 84 |
-
),
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
# Function to run FastAPI in a separate thread
|
| 88 |
-
def start_fastapi():
|
| 89 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 90 |
-
|
| 91 |
-
# Start FastAPI and Gradio
|
| 92 |
-
if __name__ == "__main__":
|
| 93 |
-
threading.Thread(target=start_fastapi).start()
|
| 94 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import uvicorn
|
| 2 |
import threading
|
| 3 |
+
import chat_fast_api
|
| 4 |
+
import gradio_app
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
def start_fast():
|
| 8 |
+
uvicorn.run("chat_fast_api:app",host="0.0.0.0",port=8000)
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
fastapi_thread = threading.Thread(target=start_fast)
|
| 11 |
+
fastapi_thread.start()
|
| 12 |
|
| 13 |
+
gradio_app.start_gradio()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|