Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
|
@@ -11,71 +13,73 @@ if not os.getenv("HUGGINGFACE_API_TOKEN"):
|
|
| 11 |
raise ValueError("Please set HUGGINGFACE_API_TOKEN in your .env file")
|
| 12 |
|
| 13 |
client = InferenceClient(
|
| 14 |
-
token=os.getenv("HUGGINGFACE_API_TOKEN")
|
| 15 |
-
timeout=10
|
| 16 |
)
|
| 17 |
|
| 18 |
MODELS = {
|
| 19 |
-
"gpt2": "
|
| 20 |
-
"
|
| 21 |
-
"
|
|
|
|
|
|
|
| 22 |
}
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
response = client.text_generation(
|
| 27 |
-
|
| 28 |
model=model_choice,
|
| 29 |
-
max_new_tokens=
|
| 30 |
temperature=0.7,
|
| 31 |
top_p=0.95,
|
| 32 |
repetition_penalty=1.15,
|
| 33 |
-
do_sample=True
|
| 34 |
-
return_full_text=False,
|
| 35 |
)
|
| 36 |
return response
|
| 37 |
except Exception as e:
|
| 38 |
-
return f"
|
| 39 |
-
|
| 40 |
-
# Create chatbot interface
|
| 41 |
-
with gr.Blocks() as interface:
|
| 42 |
-
# Model selector
|
| 43 |
-
model_choice = gr.Dropdown(
|
| 44 |
-
choices=list(MODELS.keys()),
|
| 45 |
-
value="gpt2",
|
| 46 |
-
label="π§ Select Model",
|
| 47 |
-
info="Choose the AI model (faster models listed first)"
|
| 48 |
-
)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
|
|
|
| 13 |
raise ValueError("Please set HUGGINGFACE_API_TOKEN in your .env file")
|
| 14 |
|
| 15 |
client = InferenceClient(
|
| 16 |
+
token=os.getenv("HUGGINGFACE_API_TOKEN")
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
MODELS = {
|
| 20 |
+
"gpt2": "GPT-2 (124M) - General purpose model",
|
| 21 |
+
"facebook/opt-125m": "OPT (125M) - Good for text generation",
|
| 22 |
+
"EleutherAI/pythia-160m": "Pythia (160M) - Good for code",
|
| 23 |
+
"bigscience/bloom-560m": "BLOOM (560M) - Multilingual model",
|
| 24 |
+
"microsoft/phi-1": "Phi-1 (1.3B) - Good for coding tasks"
|
| 25 |
}
|
| 26 |
|
| 27 |
+
|
| 28 |
+
# headers = {
|
| 29 |
+
# "Content-Type": "application/json",
|
| 30 |
+
# "Accept": "application/json"
|
| 31 |
+
# }
|
| 32 |
+
|
| 33 |
+
# history = []
|
| 34 |
+
|
| 35 |
+
def generate_response(prompt, model_choice):
|
| 36 |
try:
|
| 37 |
response = client.text_generation(
|
| 38 |
+
prompt,
|
| 39 |
model=model_choice,
|
| 40 |
+
max_new_tokens=400,
|
| 41 |
temperature=0.7,
|
| 42 |
top_p=0.95,
|
| 43 |
repetition_penalty=1.15,
|
| 44 |
+
do_sample=True
|
|
|
|
| 45 |
)
|
| 46 |
return response
|
| 47 |
except Exception as e:
|
| 48 |
+
return f"Error: {str(e)}\n Try with another model"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
interface = gr.Interface(
|
| 51 |
+
fn = generate_response,
|
| 52 |
+
inputs = [
|
| 53 |
+
gr.Textbox(
|
| 54 |
+
lines=20,
|
| 55 |
+
label="Let me know your questions!",
|
| 56 |
+
placeholder="You can tell me the question here..."
|
| 57 |
+
),
|
| 58 |
+
gr.Dropdown(
|
| 59 |
+
choices=list(MODELS.keys()),
|
| 60 |
+
value="microsoft/phi-1",
|
| 61 |
+
label="Select Model",
|
| 62 |
+
info="Choose the model to use"
|
| 63 |
+
)
|
| 64 |
+
],
|
| 65 |
+
outputs = gr.Textbox(
|
| 66 |
+
label="Response",
|
| 67 |
+
lines=20,
|
| 68 |
+
max_lines=40,
|
| 69 |
+
show_copy_button=True,
|
| 70 |
+
interactive=False,
|
| 71 |
+
container=True,
|
| 72 |
+
autoscroll=True,
|
| 73 |
+
scale=2
|
| 74 |
+
),
|
| 75 |
+
title="Code Assistant",
|
| 76 |
+
description="An AI assistant that helps you with coding questions using various Hugging Face models.",
|
| 77 |
+
theme="soft"
|
| 78 |
+
)
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
interface.launch(
|
| 81 |
+
share=False,
|
| 82 |
+
show_error=True,
|
| 83 |
+
server_port=int(os.getenv("PORT", 7860)),
|
| 84 |
+
show_api=False
|
| 85 |
+
)
|