Spaces:
No application file
No application file
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Install llama-cpp-python dependency
|
| 6 |
+
def install_llama_cpp():
|
| 7 |
+
try:
|
| 8 |
+
print("Installing llama-cpp-python...")
|
| 9 |
+
subprocess.check_call([
|
| 10 |
+
sys.executable, '-m', 'pip', 'install',
|
| 11 |
+
'llama-cpp-python', '--verbose'
|
| 12 |
+
])
|
| 13 |
+
print("Successfully installed llama-cpp-python")
|
| 14 |
+
return True
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error installing llama-cpp-python: {e}")
|
| 17 |
+
return False
|
| 18 |
+
|
| 19 |
+
# Install dependency first
|
| 20 |
+
install_success = install_llama_cpp()
|
| 21 |
+
|
| 22 |
+
if install_success:
|
| 23 |
+
try:
|
| 24 |
+
from llama_cpp import Llama
|
| 25 |
+
import gradio as gr
|
| 26 |
+
from huggingface_hub import hf_hub_download
|
| 27 |
+
|
| 28 |
+
print("Downloading model from your repository...")
|
| 29 |
+
|
| 30 |
+
# Download the model file from your repo with error handling
|
| 31 |
+
try:
|
| 32 |
+
model_path = hf_hub_download(
|
| 33 |
+
repo_id="Oromor/deepseek-coder-6.7b-instruct.Q4_0.gguf",
|
| 34 |
+
filename="deepseek-coder-6.7b-instruct.Q4_0.gguf"
|
| 35 |
+
)
|
| 36 |
+
print(f"Model downloaded to: {model_path}")
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Error downloading model: {e}")
|
| 39 |
+
raise
|
| 40 |
+
|
| 41 |
+
# Load the model with error handling
|
| 42 |
+
try:
|
| 43 |
+
print("Loading Llama model...")
|
| 44 |
+
model = Llama(model_path=model_path, n_ctx=4096, verbose=False)
|
| 45 |
+
print("Model loaded successfully!")
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"Error loading model: {e}")
|
| 48 |
+
raise
|
| 49 |
+
|
| 50 |
+
def predict(input_text):
|
| 51 |
+
if not input_text.strip():
|
| 52 |
+
return "Please enter some text."
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
print(f"Generating response for: {input_text[:50]}...")
|
| 56 |
+
output = model(input_text, max_tokens=1500, temperature=0.7, top_p=0.9)
|
| 57 |
+
|
| 58 |
+
if 'choices' in output and len(output['choices']) > 0:
|
| 59 |
+
response = output['choices'][0]['text']
|
| 60 |
+
return response.strip()
|
| 61 |
+
else:
|
| 62 |
+
return f"Unexpected output format: {str(output)}"
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return f"Error generating response: {str(e)}"
|
| 66 |
+
|
| 67 |
+
# Create Gradio interface
|
| 68 |
+
demo = gr.Interface(
|
| 69 |
+
fn=predict,
|
| 70 |
+
inputs=gr.Textbox(label="Your message", placeholder="Ask me anything about coding..."),
|
| 71 |
+
outputs=gr.Textbox(label="Deepseek Coder Response"),
|
| 72 |
+
title="🤖 Deepseek Coder Chat",
|
| 73 |
+
description="Chat with Deepseek Coder 6.7B model"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
print("Starting Gradio interface...")
|
| 77 |
+
demo.launch()
|
| 78 |
+
|
| 79 |
+
except ImportError as e:
|
| 80 |
+
import gradio as gr
|
| 81 |
+
|
| 82 |
+
def error_predict(input_text):
|
| 83 |
+
return f"Failed to import required libraries: {str(e)}"
|
| 84 |
+
|
| 85 |
+
demo = gr.Interface(fn=error_predict, inputs="text", outputs="text", title="Error")
|
| 86 |
+
demo.launch()
|
| 87 |
+
|
| 88 |
+
else:
|
| 89 |
+
# Fallback if installation fails
|
| 90 |
+
import gradio as gr
|
| 91 |
+
|
| 92 |
+
def error_predict(input_text):
|
| 93 |
+
return "Failed to install llama-cpp-python. Please try again or contact support."
|
| 94 |
+
|
| 95 |
+
demo = gr.Interface(fn=error_predict, inputs="text", outputs="text", title="Installation Error")
|
| 96 |
+
demo.launch()
|