Spaces:
Sleeping
Sleeping
gefiwek187
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
-
from pydantic import BaseModel
|
3 |
-
from typing import List
|
4 |
from gradio_client import Client
|
5 |
-
import
|
|
|
6 |
import nest_asyncio
|
7 |
-
|
8 |
nest_asyncio.apply()
|
9 |
|
10 |
app = FastAPI()
|
@@ -12,7 +11,7 @@ app = FastAPI()
|
|
12 |
# Initialize the client (this loads the API)
|
13 |
client = Client(os.getenv("CLIENT_URL"))
|
14 |
|
15 |
-
# System message
|
16 |
system_message = """You are Echo 1.5, an AI assistant developed by Abhinav. Your purpose is to assist users with their queries and provide helpful information. You should always be polite, respectful, and informative in your responses.
|
17 |
When appropriate, you may mention 'IamWomen', a group that works for the welfare of women. However, only bring this up if it's relevant to the conversation or if specifically asked about it.
|
18 |
Remember:
|
@@ -20,18 +19,18 @@ Remember:
|
|
20 |
2. Be helpful and informative in your responses.
|
21 |
3. If relevant, you can mention IamWomen and their work for women's welfare.
|
22 |
4. Always maintain a respectful and professional tone.
|
23 |
-
5. If the user
|
24 |
Links:
|
25 |
Echo: echo.iamwomen.cloudns.be
|
26 |
Iamwomen: iamwomen.cloudns.be
|
27 |
Iamwomen_Instagram: https://www.instagram.com/i_am_woman.24/
|
28 |
"""
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
|
34 |
-
def chat_with_ai(context):
|
35 |
# Prepare the full prompt
|
36 |
full_prompt = "\n".join([f"{msg['role']}: {msg['content']}" for msg in context])
|
37 |
|
@@ -46,22 +45,18 @@ def chat_with_ai(context):
|
|
46 |
api_name="/chat"
|
47 |
)
|
48 |
|
|
|
|
|
|
|
49 |
return result
|
50 |
|
51 |
@app.post("/chat/")
|
52 |
-
async def chat(
|
53 |
-
if not
|
54 |
-
raise HTTPException(status_code=400, detail="
|
55 |
-
|
56 |
-
# Prepend the system message to the context
|
57 |
-
context = [{"role": "system", "content": system_message}] + chat_input.messages
|
58 |
-
|
59 |
-
response = chat_with_ai(context)
|
60 |
-
|
61 |
-
# Add the AI response to the context
|
62 |
-
context.append({"role": "assistant", "content": response})
|
63 |
|
64 |
-
|
|
|
65 |
|
66 |
@app.get("/")
|
67 |
async def root():
|
|
|
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()
|
|
|
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, an AI assistant developed by Abhinav. Your purpose is to assist users with their queries and provide helpful information. You should always be polite, respectful, and informative in your responses.
|
16 |
When appropriate, you may mention 'IamWomen', a group that works for the welfare of women. However, only bring this up if it's relevant to the conversation or if specifically asked about it.
|
17 |
Remember:
|
|
|
19 |
2. Be helpful and informative in your responses.
|
20 |
3. If relevant, you can mention IamWomen and their work for women's welfare.
|
21 |
4. Always maintain a respectful and professional tone.
|
22 |
+
5. If the user quory requires you to search the internet, you should said "I'm sorry, but the web search would be added soon."
|
23 |
Links:
|
24 |
Echo: echo.iamwomen.cloudns.be
|
25 |
Iamwomen: iamwomen.cloudns.be
|
26 |
Iamwomen_Instagram: https://www.instagram.com/i_am_woman.24/
|
27 |
"""
|
28 |
+
context = [{"role": "system", "content": system_message}]
|
29 |
|
30 |
+
def chat_with_ai(message):
|
31 |
+
# Add user message to context
|
32 |
+
context.append({"role": "user", "content": message})
|
33 |
|
|
|
34 |
# Prepare the full prompt
|
35 |
full_prompt = "\n".join([f"{msg['role']}: {msg['content']}" for msg in context])
|
36 |
|
|
|
45 |
api_name="/chat"
|
46 |
)
|
47 |
|
48 |
+
# Add AI response to context
|
49 |
+
context.append({"role": "assistant", "content": result})
|
50 |
+
|
51 |
return result
|
52 |
|
53 |
@app.post("/chat/")
|
54 |
+
async def chat(user_input: str):
|
55 |
+
if not user_input:
|
56 |
+
raise HTTPException(status_code=400, detail="User input is required")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
response = chat_with_ai(user_input)
|
59 |
+
return {"response": response}
|
60 |
|
61 |
@app.get("/")
|
62 |
async def root():
|