nikunjcepatel commited on
Commit
b3c9596
·
verified ·
1 Parent(s): 520b6ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -40
app.py CHANGED
@@ -21,11 +21,7 @@ MODEL_OPTIONS = [
21
  "liquid/lfm-40b:free"
22
  ]
23
 
24
- # Initialize history
25
- history = []
26
-
27
- def generate_text(input_text, selected_model, history_state):
28
- global history
29
  response = requests.post(
30
  url="https://openrouter.ai/api/v1/chat/completions",
31
  headers={
@@ -46,46 +42,38 @@ def generate_text(input_text, selected_model, history_state):
46
 
47
  # Handle errors
48
  if response.status_code != 200:
49
- return f"Error: {response.status_code}, {response.text}", history_state
50
 
51
  # Parse and return the content of the response
52
  try:
53
  response_json = response.json()
54
- result = response_json.get("choices", [{}])[0].get("message", {}).get("content", "No content returned.")
55
  except json.JSONDecodeError:
56
- result = "Error: Unable to parse response."
57
-
58
- # Add the current interaction to the history
59
- history_entry = {
60
- "input": input_text,
61
- "selected_model": selected_model,
62
- "response": result
63
- }
64
- history.append(history_entry)
65
-
66
- # Update the history state
67
- history_state = history
 
 
 
 
 
 
68
 
69
- # Prepare the formatted history string
70
- formatted_history = "\n".join([f"Input: {entry['input']}\nModel: {entry['selected_model']}\nResponse: {entry['response']}\n" for entry in history])
71
 
72
- return result, formatted_history, history_state
73
-
74
- # Create Gradio interface with a dropdown for model selection
75
- iface = gr.Interface(
76
- fn=generate_text,
77
- inputs=[
78
- gr.Textbox(lines=2, label="Input Text", placeholder="Enter your query here"),
79
- gr.Dropdown(choices=MODEL_OPTIONS, label="Select Model", value=MODEL_OPTIONS[0]),
80
- gr.State()
81
- ],
82
- outputs=[
83
- gr.Textbox(label="Response", placeholder="Response will be shown here"),
84
- gr.Textbox(label="History", placeholder="Interaction history will be shown here", lines=10, interactive=False),
85
- gr.State()
86
- ],
87
- title="Chat with OpenRouter Models"
88
- )
89
 
90
- # Directly include custom CSS with HTML component
91
- iface.launch(inline_css=True)
 
21
  "liquid/lfm-40b:free"
22
  ]
23
 
24
+ def generate_text(input_text, selected_model):
 
 
 
 
25
  response = requests.post(
26
  url="https://openrouter.ai/api/v1/chat/completions",
27
  headers={
 
42
 
43
  # Handle errors
44
  if response.status_code != 200:
45
+ return f"Error: {response.status_code}, {response.text}"
46
 
47
  # Parse and return the content of the response
48
  try:
49
  response_json = response.json()
50
+ return response_json.get("choices", [{}])[0].get("message", {}).get("content", "No content returned.")
51
  except json.JSONDecodeError:
52
+ return "Error: Unable to parse response."
53
+
54
+ # Define the Gradio layout using Blocks
55
+ with gr.Blocks() as iface:
56
+ # Inject custom CSS using gr.HTML()
57
+ gr.HTML("""
58
+ <style>
59
+ #output-response, #output-history {
60
+ height: 300px;
61
+ overflow: auto;
62
+ border: 1px solid #ddd;
63
+ padding: 10px;
64
+ }
65
+ </style>
66
+ """)
67
+
68
+ input_text = gr.Textbox(lines=2, label="Input Text", placeholder="Enter your query here")
69
+ selected_model = gr.Dropdown(choices=MODEL_OPTIONS, label="Select Model", value=MODEL_OPTIONS[0])
70
 
71
+ output_response = gr.Textbox(label="Response", placeholder="Response will be shown here")
72
+ output_history = gr.Textbox(label="History", placeholder="Interaction history will be shown here", lines=10, interactive=False)
73
 
74
+ # Trigger the function when the user inputs text
75
+ generate_button = gr.Button("Generate")
76
+ generate_button.click(generate_text, inputs=[input_text, selected_model], outputs=[output_response, output_history])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
+ # Launch the interface
79
+ iface.launch()