Spaces:
Sleeping
Sleeping
File size: 1,342 Bytes
977465c 77335d6 9f99956 d9c9140 1c5429b 9f99956 77335d6 72f98bf be8d0a1 9f99956 977465c 9f99956 977465c 9f99956 977465c 9f99956 977465c 9f99956 977465c 9f99956 977465c be8d0a1 9f99956 77335d6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import os
from fastapi import FastAPI, Request
from pydantic import BaseModel
import dashscope
from dashscope import Generation
# Set up the API key for Dashscope
YOUR_API_TOKEN = os.getenv('HF_TOKEN')
dashscope.api_key = YOUR_API_TOKEN
app = FastAPI()
math_messages = []
class MathQuery(BaseModel):
question: str
def get_math_response(user_question):
global math_messages
if not math_messages:
math_messages.append({'role': 'system', 'content': 'You are a helpful math assistant.'})
math_messages = math_messages[:1]
query = f"User question: {user_question}"
math_messages.append({'role': 'user', 'content': query})
response = Generation.call(
model="qwen2-math-72b-instruct",
messages=math_messages,
result_format='message',
stream=False
)
answer = response.output.choices[0].message.content if response.output else "No response available."
if answer:
math_messages.append({'role': 'assistant', 'content': answer})
return answer
@app.post("/math-solver")
async def solve_math(query: MathQuery):
user_question = query.question
answer = get_math_response(user_question)
return {"question": user_question, "answer": answer}
@app.get("/")
def root():
return {"message": "Welcome to the FastAPI Math Solver!"}
|