Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
demo.launch()
|
|
|
|
| 1 |
+
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Ollama API endpoint
|
| 7 |
+
url = "http://localhost:11434/api/generate"
|
| 8 |
+
|
| 9 |
+
headers = {
|
| 10 |
+
'Content-Type': 'application/json'
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
# For maintaining conversation history
|
| 14 |
+
history = []
|
| 15 |
+
|
| 16 |
+
def generate_response(prompt):
|
| 17 |
+
history.append(f"User: {prompt}")
|
| 18 |
+
final_prompt = "\n".join(history)
|
| 19 |
+
|
| 20 |
+
# Request payload for Ollama
|
| 21 |
+
data = {
|
| 22 |
+
"model": "zencode",
|
| 23 |
+
"prompt": final_prompt,
|
| 24 |
+
"stream": False
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
response = requests.post(url, headers=headers, data=json.dumps(data))
|
| 29 |
+
|
| 30 |
+
if response.status_code == 200:
|
| 31 |
+
data = response.json()
|
| 32 |
+
actual_response = data.get('response', 'No response found.')
|
| 33 |
+
|
| 34 |
+
history.append(f"Zencode: {actual_response}")
|
| 35 |
+
return actual_response
|
| 36 |
+
else:
|
| 37 |
+
return f"❌ Error: {response.status_code} - {response.text}"
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"❌ Exception: {str(e)}"
|
| 41 |
+
|
| 42 |
+
# Gradio interface
|
| 43 |
+
interface = gr.Interface(
|
| 44 |
+
fn=generate_response,
|
| 45 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter your prompt here...", label="Your Question"),
|
| 46 |
+
outputs=gr.Textbox(label="Zencode's Response"),
|
| 47 |
+
title="💡 Zencode - AI Coding Assistant"
|
| 48 |
+
)
|
| 49 |
|
| 50 |
+
interface.launch()
|
|
|