Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,45 @@
|
|
| 1 |
-
|
| 2 |
import os
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
from dashscope import MultiModalConversation, Generation
|
| 8 |
-
|
| 9 |
-
# Set up the API key for Dashscope
|
| 10 |
-
headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
|
| 11 |
-
dashscope.api_key = headers
|
| 12 |
-
print(dashscope.api_key)
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
question
|
|
|
|
| 20 |
|
| 21 |
def get_math_response(user_question):
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
math_messages.append({'role': 'system', 'content': 'You are a helpful math assistant.'})
|
| 25 |
-
math_messages = math_messages[:1]
|
| 26 |
|
|
|
|
| 27 |
query = f"User question: {user_question}"
|
| 28 |
math_messages.append({'role': 'user', 'content': query})
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
model="qwen2-math-72b-instruct",
|
| 32 |
-
messages=math_messages,
|
| 33 |
result_format='message',
|
| 34 |
-
stream=
|
| 35 |
)
|
| 36 |
|
| 37 |
-
answer =
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
|
|
|
|
|
|
| 41 |
return answer
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
user_question = query.question
|
| 46 |
-
answer = get_math_response(user_question)
|
| 47 |
-
return {"question": user_question, "answer": answer}
|
| 48 |
-
|
| 49 |
-
# Optional: Handle 404 errors gracefully without defining additional routes
|
| 50 |
-
@app.exception_handler(HTTPException)
|
| 51 |
-
async def http_exception_handler(request, exc):
|
| 52 |
-
if exc.status_code == 404:
|
| 53 |
-
return {"detail": "Not Found. Use /math-solver to submit a math question."}
|
| 54 |
-
return await request.app.default_exception_handler(request, exc)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Form
|
| 2 |
import os
|
| 3 |
+
from dashscope import Generation
|
| 4 |
|
| 5 |
+
# Set API key
|
| 6 |
+
YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
|
| 7 |
+
dashscope.api_key = YOUR_API_TOKEN
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
@app.post("/math")
|
| 12 |
+
async def math_assistant(question: str = Form(...)):
|
| 13 |
+
# Call the Qwen models for math assistance
|
| 14 |
+
math_response = get_math_response(question)
|
| 15 |
+
return {"answer": math_response}
|
| 16 |
|
| 17 |
def get_math_response(user_question):
|
| 18 |
+
# Reset math messages for each question
|
| 19 |
+
math_messages = [{'role': 'system', 'content': 'You are a helpful math assistant.'}]
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Construct the query
|
| 22 |
query = f"User question: {user_question}"
|
| 23 |
math_messages.append({'role': 'user', 'content': query})
|
| 24 |
|
| 25 |
+
# Call the math model
|
| 26 |
+
response = Generation.call(
|
| 27 |
model="qwen2-math-72b-instruct",
|
| 28 |
+
messages=math_messages,
|
| 29 |
result_format='message',
|
| 30 |
+
stream=True
|
| 31 |
)
|
| 32 |
|
| 33 |
+
answer = None
|
| 34 |
+
for resp in response:
|
| 35 |
+
if resp.output is None:
|
| 36 |
+
continue
|
| 37 |
+
answer = resp.output.choices[0].message.content
|
| 38 |
+
yield answer.replace("\\", "\\\\")
|
| 39 |
|
| 40 |
+
if answer is None:
|
| 41 |
+
return "No answer found."
|
| 42 |
return answer
|
| 43 |
|
| 44 |
+
# To run the application, use:
|
| 45 |
+
# uvicorn app:app --host 0.0.0.0 --port 8000 --reload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|