Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,16 @@
|
|
| 1 |
-
import
|
| 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 |
-
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
class MathQuery(BaseModel):
|
| 16 |
-
question: str
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 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.
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
return {"
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
+
# Load the text generation pipeline from Hugging Face
|
| 6 |
+
generator = pipeline('text-generation', model='gpt2')
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
@app.get("/")
|
| 9 |
+
def greet_json():
|
| 10 |
+
return {"Hello": "World!from fastapi import FastAPI"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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']}
|