Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,45 @@
|
|
| 1 |
-
|
| 2 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
@app.
|
| 9 |
-
def
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
@app.get("/")
|
| 13 |
-
def generate_text():
|
| 14 |
-
# Generate text from the model
|
| 15 |
-
generated = generator("once upaon aa time", max_length=100, num_return_sequences=1)
|
| 16 |
-
return {"generated_text": generated[0]['generated_text']}
|
|
|
|
| 1 |
+
import os
|
| 2 |
from fastapi import FastAPI
|
| 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')
|
| 9 |
+
dashscope.api_key = YOUR_API_TOKEN
|
| 10 |
+
print(dashscope.api_key)
|
| 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|