Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,51 @@
|
|
| 1 |
-
from fastapi import FastAPI, Form
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
| 3 |
from dashscope import Generation
|
| 4 |
|
| 5 |
-
# Set API key
|
| 6 |
-
|
| 7 |
-
dashscope.api_key
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
return {"answer": math_response}
|
| 16 |
|
| 17 |
def get_math_response(user_question):
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# Construct the query
|
| 22 |
query = f"User question: {user_question}"
|
| 23 |
math_messages.append({'role': 'user', 'content': query})
|
| 24 |
|
| 25 |
-
|
| 26 |
-
response = Generation.call(
|
| 27 |
model="qwen2-math-72b-instruct",
|
| 28 |
-
messages=math_messages,
|
| 29 |
result_format='message',
|
| 30 |
-
stream=
|
| 31 |
)
|
| 32 |
|
| 33 |
-
answer =
|
| 34 |
-
|
| 35 |
-
|
| 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 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import dashscope
|
| 5 |
from dashscope import Generation
|
| 6 |
|
| 7 |
+
# Set up the API key for Dashscope
|
| 8 |
+
dashscope.api_key = os.getenv('HF_TOKEN') # Ensure the environment variable is set
|
| 9 |
+
print(dashscope.api_key)
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
+
math_messages = []
|
| 14 |
+
|
| 15 |
+
class MathQuery(BaseModel):
|
| 16 |
+
question: str
|
|
|
|
| 17 |
|
| 18 |
def get_math_response(user_question):
|
| 19 |
+
global math_messages
|
| 20 |
+
if not math_messages:
|
| 21 |
+
math_messages.append({'role': 'system', 'content': 'You are a helpful math assistant.'})
|
| 22 |
+
math_messages = math_messages[:1]
|
| 23 |
|
|
|
|
| 24 |
query = f"User question: {user_question}"
|
| 25 |
math_messages.append({'role': 'user', 'content': query})
|
| 26 |
|
| 27 |
+
response = Generation.call(
|
|
|
|
| 28 |
model="qwen2-math-72b-instruct",
|
| 29 |
+
messages=math_messages,
|
| 30 |
result_format='message',
|
| 31 |
+
stream=False
|
| 32 |
)
|
| 33 |
|
| 34 |
+
answer = response.output.choices[0].message.content if response.output else "No response available."
|
| 35 |
+
if answer:
|
| 36 |
+
math_messages.append({'role': 'assistant', 'content': answer})
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
|
|
|
| 38 |
return answer
|
| 39 |
|
| 40 |
+
@app.post("/math-solver")
|
| 41 |
+
async def solve_math(query: MathQuery):
|
| 42 |
+
user_question = query.question
|
| 43 |
+
answer = get_math_response(user_question)
|
| 44 |
+
return {"question": user_question, "answer": answer}
|
| 45 |
+
|
| 46 |
+
# Optional: Handle 404 errors gracefully without defining additional routes
|
| 47 |
+
@app.exception_handler(HTTPException)
|
| 48 |
+
async def http_exception_handler(request, exc):
|
| 49 |
+
if exc.status_code == 404:
|
| 50 |
+
return {"detail": "Not Found. Use /math-solver to submit a math question."}
|
| 51 |
+
return await request.app.default_exception_handler(request, exc)
|