|
from flask import Flask, request, jsonify |
|
from transformers import pipeline |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
model_pipeline = pipeline( |
|
"text-generation", |
|
model="Omartificial-Intelligence-Space/Arabic-QWQ-32B-Preview" |
|
) |
|
|
|
@app.route('/') |
|
def index(): |
|
"""Root endpoint, can serve an HTML form if desired.""" |
|
return """ |
|
<h1>Arabic-QwQ Model Demo</h1> |
|
<form action="/predict" method="post"> |
|
<label>Enter your prompt:</label><br> |
|
<input type="text" name="prompt" required><br><br> |
|
<input type="submit" value="Submit"> |
|
</form> |
|
""" |
|
|
|
@app.route('/predict', methods=["POST"]) |
|
def predict(): |
|
""" |
|
Route for processing user input with the model. |
|
- Accepts user input via POST request. |
|
- Runs inference with Arabic-QwQ model. |
|
- Returns response. |
|
""" |
|
try: |
|
|
|
user_input = request.form.get("prompt") |
|
|
|
|
|
output = model_pipeline(user_input, max_length=50, num_return_sequences=1) |
|
|
|
|
|
return jsonify({ |
|
"input": user_input, |
|
"response": output[0]['generated_text'] if output else "No response generated" |
|
}) |
|
|
|
except Exception as e: |
|
|
|
return jsonify({"error": str(e)}), 500 |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
app.run(debug=True) |
|
|