Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,49 @@
|
|
| 1 |
-
import os
|
| 2 |
import subprocess
|
| 3 |
import sys
|
| 4 |
-
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
-
# Function to ensure
|
| 8 |
def install_and_import(package):
|
| 9 |
try:
|
| 10 |
__import__(package)
|
| 11 |
except ImportError:
|
| 12 |
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 13 |
|
| 14 |
-
# Install transformers and
|
| 15 |
install_and_import("transformers")
|
| 16 |
-
install_and_import("
|
| 17 |
-
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
print("Model loaded successfully.")
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
@app.route("/", methods=["GET"])
|
| 30 |
-
def home():
|
| 31 |
-
return jsonify({"message": "Welcome to Gardio with Transformers!"})
|
| 32 |
|
| 33 |
-
|
| 34 |
-
def
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return jsonify({"error": "Prompt is required"}), 400
|
| 40 |
-
|
| 41 |
-
# Generate text using the model
|
| 42 |
-
result = model(prompt, max_length=50, num_return_sequences=1)
|
| 43 |
-
return jsonify({"generated_text": result[0]["generated_text"]})
|
| 44 |
-
|
| 45 |
except Exception as e:
|
| 46 |
-
return
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
if __name__ == "__main__":
|
| 49 |
-
|
| 50 |
-
app.run(host="0.0.0.0", port=port)
|
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import sys
|
| 3 |
+
import gradio as gr
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
+
# Function to ensure required libraries are installed
|
| 7 |
def install_and_import(package):
|
| 8 |
try:
|
| 9 |
__import__(package)
|
| 10 |
except ImportError:
|
| 11 |
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 12 |
|
| 13 |
+
# Install transformers and gradio if not installed
|
| 14 |
install_and_import("transformers")
|
| 15 |
+
install_and_import("gradio")
|
| 16 |
+
|
| 17 |
+
# Load DeepSeek-R1 model pipeline
|
| 18 |
+
def load_pipeline():
|
| 19 |
+
global pipe
|
| 20 |
+
pipe = pipeline(
|
| 21 |
+
"text-generation",
|
| 22 |
+
model="deepseek-ai/DeepSeek-R1",
|
| 23 |
+
trust_remote_code=True
|
| 24 |
+
)
|
| 25 |
print("Model loaded successfully.")
|
| 26 |
|
| 27 |
+
load_pipeline()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Function to handle user prompts and generate responses
|
| 30 |
+
def generate_response(prompt):
|
| 31 |
try:
|
| 32 |
+
# Generate response from the model
|
| 33 |
+
response = pipe(prompt, max_length=100, num_return_sequences=1)
|
| 34 |
+
return response[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
+
return f"Error: {str(e)}"
|
| 37 |
+
|
| 38 |
+
# Gradio Interface
|
| 39 |
+
interface = gr.Interface(
|
| 40 |
+
fn=generate_response,
|
| 41 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
|
| 42 |
+
outputs=gr.Textbox(label="Generated Text"),
|
| 43 |
+
title="DeepSeek-R1 Text Generator",
|
| 44 |
+
description="Generate text using the DeepSeek-R1 model hosted on Hugging Face.",
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Launch the Gradio app
|
| 48 |
if __name__ == "__main__":
|
| 49 |
+
interface.launch()
|
|
|