richardkimsm89 commited on
Commit
5f71f8b
·
verified ·
1 Parent(s): 4505847

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -27
app.py CHANGED
@@ -46,42 +46,39 @@ import gradio as gr
46
  from huggingface_hub import InferenceClient
47
  import os
48
 
49
- # Initialize Hugging Face Inference Client
50
  hf_token = os.getenv("HF_TOKEN")
51
  client = InferenceClient(api_key=hf_token)
52
 
53
- # Function to handle user inputs and fetch model responses
54
- def chatbot(input_text, history=[]):
55
- #messages = [{"role": "user", "content": input_text}]
56
  messages = []
57
- for user_input, bot_response in history:
58
- messages.append({"role": "user", "content": user_input})
59
- messages.append({"role": "assistant", "content": bot_response})
60
- messages.append({"role": "user", "content": input_text})
 
 
61
 
62
  stream = client.chat.completions.create(
63
- model="google/gemma-2-2b-it",
64
- messages=messages,
65
- #temperature=0.5,
66
- #max_tokens=2048,
67
- #top_p=0.7,
68
- stream=True
69
  )
70
 
71
- # Concatenate streamed response
72
  bot_response = "".join(chunk.choices[0].delta.content for chunk in stream)
73
 
74
- # Update conversation history
75
- history.append((input_text, bot_response))
76
  return bot_response, history
77
 
78
- # Gradio Interface
79
- demo = gr.Interface(
80
- fn=chatbot,
81
- inputs=["text", "state"],
82
- outputs=["text", "state"],
83
- title="Gemma Chatbot"
84
- )
85
-
86
- # Launch Gradio App
87
- demo.launch()
 
46
  from huggingface_hub import InferenceClient
47
  import os
48
 
 
49
  hf_token = os.getenv("HF_TOKEN")
50
  client = InferenceClient(api_key=hf_token)
51
 
52
+ def fn(prompt, history=[]):
 
 
53
  messages = []
54
+
55
+ for user_prompt, bot_response in history:
56
+ messages.append({"role": "user", "content": user_prompt})
57
+ messages.append({"role": "bot", "content": bot_response})
58
+
59
+ messages.append({"role": "user", "content": prompt})
60
 
61
  stream = client.chat.completions.create(
62
+ model = "google/gemma-2-2b-it",
63
+ messages = messages,
64
+ #temperature = 0.5,
65
+ #max_tokens = 2048,
66
+ #top_p = 0.7,
67
+ stream = True
68
  )
69
 
 
70
  bot_response = "".join(chunk.choices[0].delta.content for chunk in stream)
71
 
72
+ history.append((prompt, bot_response))
 
73
  return bot_response, history
74
 
75
+ app = gr.Interface(
76
+ fn = fn,
77
+ inputs = [gr.Textbox(label = "Input")],
78
+ outputs = [gr.Textbox(label = "Output")],
79
+ title = "Google Gemma",
80
+ description = "Chatbot",
81
+ examples = [
82
+ ["Hello, World."]
83
+ ]
84
+ ).launch()