Upload 2 files
Browse files- app.py +0 -64
- rivan app.py +116 -0
app.py
CHANGED
|
@@ -1,64 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
|
| 4 |
-
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rivan app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# ๐ Insert your OpenAI API key here (keep it secure)
|
| 5 |
+
openai.api_key = "your-openai-api-key"
|
| 6 |
+
|
| 7 |
+
# Store chat history
|
| 8 |
+
chat_history = []
|
| 9 |
+
|
| 10 |
+
# GPT-3.5 Turbo model call
|
| 11 |
+
def chat_with_gpt35(user_input, system_role="You are a helpful and expert AI assistant for technical topics. Answer clearly and concisely."):
|
| 12 |
+
global chat_history
|
| 13 |
+
|
| 14 |
+
# Add user message to history
|
| 15 |
+
chat_history.append({"role": "user", "content": user_input})
|
| 16 |
+
|
| 17 |
+
# Send the conversation to OpenAI
|
| 18 |
+
response = openai.ChatCompletion.create(
|
| 19 |
+
model="gpt-3.5-turbo",
|
| 20 |
+
messages=[{"role": "system", "content": system_role}] + chat_history,
|
| 21 |
+
temperature=0.7,
|
| 22 |
+
top_p=0.9,
|
| 23 |
+
max_tokens=600
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Extract and store assistant's reply
|
| 27 |
+
reply = response['choices'][0]['message']['content'].strip()
|
| 28 |
+
chat_history.append({"role": "assistant", "content": reply})
|
| 29 |
+
|
| 30 |
+
# Display last 6 messages (3 rounds)
|
| 31 |
+
display = []
|
| 32 |
+
for msg in chat_history[-6:]:
|
| 33 |
+
role = "๐งโ๐ป User" if msg["role"] == "user" else "๐ค RIVAN"
|
| 34 |
+
display.append(f"{role}: {msg['content']}")
|
| 35 |
+
|
| 36 |
+
return "\n\n".join(display)
|
| 37 |
+
|
| 38 |
+
# Reset conversation
|
| 39 |
+
def reset_chat():
|
| 40 |
+
global chat_history
|
| 41 |
+
chat_history = []
|
| 42 |
+
return ""
|
| 43 |
+
|
| 44 |
+
# Background image URL
|
| 45 |
+
background_url = "https://images.unsplash.com/photo-1507525428034-b723cf961d3e"
|
| 46 |
+
|
| 47 |
+
# Build Gradio UI
|
| 48 |
+
with gr.Blocks(title="RIVAN - GPT-3.5 Technical Chatbot") as app:
|
| 49 |
+
gr.HTML(f"""
|
| 50 |
+
<style>
|
| 51 |
+
body {{
|
| 52 |
+
background-image: url("{background_url}");
|
| 53 |
+
background-size: cover;
|
| 54 |
+
background-repeat: no-repeat;
|
| 55 |
+
background-attachment: fixed;
|
| 56 |
+
background-position: center;
|
| 57 |
+
}}
|
| 58 |
+
.gradio-container {{
|
| 59 |
+
background: rgba(255, 255, 255, 0.92);
|
| 60 |
+
padding: 20px;
|
| 61 |
+
border-radius: 12px;
|
| 62 |
+
}}
|
| 63 |
+
</style>
|
| 64 |
+
""")
|
| 65 |
+
|
| 66 |
+
gr.Markdown("## ๐ค RIVAN - Technical Chatbot Powered by GPT-3.5")
|
| 67 |
+
|
| 68 |
+
chat_output = gr.Textbox(label="Conversation", lines=20, interactive=False)
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
user_input = gr.Textbox(lines=2, label="Ask a technical question")
|
| 72 |
+
send_btn = gr.Button("Ask")
|
| 73 |
+
|
| 74 |
+
reset_btn = gr.Button("Reset Conversation")
|
| 75 |
+
|
| 76 |
+
send_btn.click(chat_with_gpt35, inputs=[user_input], outputs=[chat_output])
|
| 77 |
+
reset_btn.click(reset_chat, outputs=[chat_output])
|
| 78 |
+
|
| 79 |
+
app.launch()'''
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 85 |
+
import gradio as gr
|
| 86 |
+
|
| 87 |
+
# Load GPT-2 model and tokenizer
|
| 88 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
| 89 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 90 |
+
|
| 91 |
+
# Define text generation function
|
| 92 |
+
def generate_text(prompt, max_length=50, temperature=1.0, top_p=0.95):
|
| 93 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 94 |
+
outputs = model.generate(
|
| 95 |
+
inputs.input_ids,
|
| 96 |
+
max_length=max_length,
|
| 97 |
+
do_sample=True,
|
| 98 |
+
temperature=temperature,
|
| 99 |
+
top_p=top_p,
|
| 100 |
+
pad_token_id=tokenizer.eos_token_id
|
| 101 |
+
)
|
| 102 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 103 |
+
|
| 104 |
+
# Gradio interface
|
| 105 |
+
gr.Interface(
|
| 106 |
+
fn=generate_text,
|
| 107 |
+
inputs=[
|
| 108 |
+
gr.Textbox(lines=2, label="Enter your prompt"),
|
| 109 |
+
gr.Slider(20, 100, value=50, step=10, label="Max Length"),
|
| 110 |
+
gr.Slider(0.1, 2.0, value=1.0, step=0.1, label="Temperature"),
|
| 111 |
+
gr.Slider(0.5, 1.0, value=0.95, step=0.05, label="Top-p")
|
| 112 |
+
],
|
| 113 |
+
outputs="textbox",
|
| 114 |
+
title="๐ฎ GPT-2 Text Generator",
|
| 115 |
+
description="Give a prompt and let AI continue writing it for you."
|
| 116 |
+
).launch()
|