arshadrana commited on
Commit
be8d0a1
·
verified ·
1 Parent(s): 6fefe4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py CHANGED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI, Request
3
+ from pydantic import BaseModel
4
+ import dashscope
5
+ from dashscope import Generation
6
+
7
+ # Set up the API key for Dashscope
8
+ YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
9
+ dashscope.api_key = YOUR_API_TOKEN
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
+ @app.get("/")
47
+ def root():
48
+ return {"message": "Welcome to the FastAPI Math Solver!"}