Spaces:
Sleeping
Sleeping
gefiwek187
commited on
Commit
•
b8257f6
1
Parent(s):
539d13c
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from gradio_client import Client
|
3 |
+
import time
|
4 |
+
import asyncio
|
5 |
+
import nest_asyncio
|
6 |
+
import os
|
7 |
+
nest_asyncio.apply()
|
8 |
+
|
9 |
+
app = FastAPI()
|
10 |
+
|
11 |
+
# Initialize the client (this loads the API)
|
12 |
+
client = Client(os.getenv("CLIENT_URL"))
|
13 |
+
|
14 |
+
# System message and initial context
|
15 |
+
system_message = "You are Echo 1.5, developed by Abhinav in collaboration with Iamwomen, a group dedicated to the welfare of women."
|
16 |
+
context = [{"role": "system", "content": system_message}]
|
17 |
+
|
18 |
+
def chat_with_ai(message):
|
19 |
+
# Add user message to context
|
20 |
+
context.append({"role": "user", "content": message})
|
21 |
+
|
22 |
+
# Prepare the full prompt
|
23 |
+
full_prompt = "\n".join([f"{msg['role']}: {msg['content']}" for msg in context])
|
24 |
+
|
25 |
+
# Make the API call
|
26 |
+
result = client.predict(
|
27 |
+
message=full_prompt,
|
28 |
+
request=os.getenv('MODEL'),
|
29 |
+
param_3=0.5,
|
30 |
+
param_4=4096,
|
31 |
+
param_5=0.5,
|
32 |
+
param_6=0,
|
33 |
+
api_name="/chat"
|
34 |
+
)
|
35 |
+
|
36 |
+
# Add AI response to context
|
37 |
+
context.append({"role": "assistant", "content": result})
|
38 |
+
|
39 |
+
return result
|
40 |
+
|
41 |
+
@app.post("/chat/")
|
42 |
+
async def chat(user_input: str):
|
43 |
+
if not user_input:
|
44 |
+
raise HTTPException(status_code=400, detail="User input is required")
|
45 |
+
|
46 |
+
response = chat_with_ai(user_input)
|
47 |
+
return {"response": response}
|
48 |
+
|
49 |
+
@app.get("/")
|
50 |
+
async def root():
|
51 |
+
return {"message": "Running"}
|
52 |
+
|
53 |
+
if __name__ == "__main__":
|
54 |
+
import uvicorn
|
55 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|