arshadrana commited on
Commit
9f99956
·
verified ·
1 Parent(s): cd2d859

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -26
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
- YOUR_API_TOKEN = os.getenv('HF_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
 
 
 
 
 
 
 
 
 
 
 
 
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)