Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
|
| 5 |
+
class CodingAssistant:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.client = InferenceClient(
|
| 8 |
+
model="codellama/CodeLlama-7b-Instruct-hf", # Your custom model here
|
| 9 |
+
token=os.getenv("HF_TOKEN")
|
| 10 |
+
)
|
| 11 |
+
self.chat_history = []
|
| 12 |
+
|
| 13 |
+
def respond(self, message, history):
|
| 14 |
+
# Build prompt with conversation context
|
| 15 |
+
prompt = f"""<s>[INST] <<SYS>>
|
| 16 |
+
You are an expert Python programmer. Provide safe, efficient code solutions.
|
| 17 |
+
Maintain conversation history: {self.chat_history[-3:] if self.chat_history else 'None'}
|
| 18 |
+
<</SYS>> {message} [/INST]"""
|
| 19 |
+
|
| 20 |
+
# Generate response
|
| 21 |
+
response = self.client.text_generation(
|
| 22 |
+
prompt=prompt,
|
| 23 |
+
max_new_tokens=1024,
|
| 24 |
+
temperature=0.2,
|
| 25 |
+
repetition_penalty=1.1
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Update history
|
| 29 |
+
self.chat_history.append((message, response))
|
| 30 |
+
return response
|
| 31 |
+
|
| 32 |
+
# Initialize assistant
|
| 33 |
+
assistant = CodingAssistant()
|
| 34 |
+
|
| 35 |
+
# Create Gradio interface
|
| 36 |
+
demo = gr.ChatInterface(
|
| 37 |
+
fn=assistant.respond,
|
| 38 |
+
examples=[
|
| 39 |
+
"How to implement a neural network in PyTorch?",
|
| 40 |
+
"Write a Python decorator for rate limiting",
|
| 41 |
+
"Optimize this pandas code: ..."
|
| 42 |
+
],
|
| 43 |
+
title="Code Expert Assistant",
|
| 44 |
+
description="Ask me complex Python programming questions",
|
| 45 |
+
theme="soft",
|
| 46 |
+
retry_btn=None,
|
| 47 |
+
undo_btn=None
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
demo.launch()
|