Upload 2 files
Browse filesFiles changed (2)
- app.py-64
- rivan app.pyAdded+116
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import gradio as grfrom huggingface_hub import InferenceClient"""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"""client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")def respond(message,history: list[tuple[str, str]],system_message,max_tokens,temperature,top_p,):messages = [{"role": "system", "content": system_message}]for val in history:if val[0]:messages.append({"role": "user", "content": val[0]})if val[1]:messages.append({"role": "assistant", "content": val[1]})messages.append({"role": "user", "content": message})response = ""for message in client.chat_completion(messages,max_tokens=max_tokens,stream=True,temperature=temperature,top_p=top_p,):token = message.choices[0].delta.contentresponse += tokenyield response"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface(respond,additional_inputs=[gr.Textbox(value="You are a friendly Chatbot.", label="System message"),gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),gr.Slider(minimum=0.1,maximum=1.0,value=0.95,step=0.05,label="Top-p (nucleus sampling)",),],)if __name__ == "__main__":demo.launch()
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116'''import openai
import gradio as gr
# ๐ Insert your OpenAI API key here (keep it secure)
openai.api_key = "your-openai-api-key"
# Store chat history
chat_history = []
# GPT-3.5 Turbo model call
def chat_with_gpt35(user_input, system_role="You are a helpful and expert AI assistant for technical topics. Answer clearly and concisely."):
global chat_history
# Add user message to history
chat_history.append({"role": "user", "content": user_input})
# Send the conversation to OpenAI
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "system", "content": system_role}] + chat_history,
temperature=0.7,
top_p=0.9,
max_tokens=600
)
# Extract and store assistant's reply
reply = response['choices'][0]['message']['content'].strip()
chat_history.append({"role": "assistant", "content": reply})
# Display last 6 messages (3 rounds)
display = []
for msg in chat_history[-6:]:
role = "๐งโ๐ป User" if msg["role"] == "user" else "๐ค RIVAN"
display.append(f"{role}: {msg['content']}")
return "\n\n".join(display)
# Reset conversation
def reset_chat():
global chat_history
chat_history = []
return ""
# Background image URL
background_url = "https://images.unsplash.com/photo-1507525428034-b723cf961d3e"
# Build Gradio UI
with gr.Blocks(title="RIVAN - GPT-3.5 Technical Chatbot") as app:
gr.HTML(f"""
<style>
body {{
background-image: url("{background_url}");
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}}
.gradio-container {{
background: rgba(255, 255, 255, 0.92);
padding: 20px;
border-radius: 12px;
}}
</style>
""")
gr.Markdown("## ๐ค RIVAN - Technical Chatbot Powered by GPT-3.5")
chat_output = gr.Textbox(label="Conversation", lines=20, interactive=False)
with gr.Row():
user_input = gr.Textbox(lines=2, label="Ask a technical question")
send_btn = gr.Button("Ask")
reset_btn = gr.Button("Reset Conversation")
send_btn.click(chat_with_gpt35, inputs=[user_input], outputs=[chat_output])
reset_btn.click(reset_chat, outputs=[chat_output])
app.launch()'''
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import gradio as gr
# Load GPT-2 model and tokenizer
model = GPT2LMHeadModel.from_pretrained("gpt2")
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
# Define text generation function
def generate_text(prompt, max_length=50, temperature=1.0, top_p=0.95):
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(
inputs.input_ids,
max_length=max_length,
do_sample=True,
temperature=temperature,
top_p=top_p,
pad_token_id=tokenizer.eos_token_id
)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
# Gradio interface
gr.Interface(
fn=generate_text,
inputs=[
gr.Textbox(lines=2, label="Enter your prompt"),
gr.Slider(20, 100, value=50, step=10, label="Max Length"),
gr.Slider(0.1, 2.0, value=1.0, step=0.1, label="Temperature"),
gr.Slider(0.5, 1.0, value=0.95, step=0.05, label="Top-p")
],
outputs="textbox",
title="๐ฎ GPT-2 Text Generator",
description="Give a prompt and let AI continue writing it for you."
).launch()app.py-64
rivan app.pyAdded+116