arshadrana commited on
Commit
d9c9140
·
verified ·
1 Parent(s): 756b7c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -36
app.py CHANGED
@@ -1,54 +1,45 @@
1
-
2
  import os
 
3
 
4
- from fastapi import FastAPI, HTTPException
5
- from pydantic import BaseModel
6
- import dashscope
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
- math_messages = []
17
-
18
- class MathQuery(BaseModel):
19
- question: str
 
20
 
21
  def get_math_response(user_question):
22
- global math_messages
23
- if not math_messages:
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
- response = Generation.call(
 
31
  model="qwen2-math-72b-instruct",
32
- messages=math_messages,
33
  result_format='message',
34
- stream=False
35
  )
36
 
37
- answer = response.output.choices[0].message.content if response.output else "No response available."
38
- if answer:
39
- math_messages.append({'role': 'assistant', 'content': answer})
 
 
 
40
 
 
 
41
  return answer
42
 
43
- @app.post("/math-solver")
44
- async def solve_math(query: MathQuery):
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