Dua Rajper commited on
Commit
8288621
·
verified ·
1 Parent(s): 2a648ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -31
app.py CHANGED
@@ -1,45 +1,38 @@
1
  import os
2
  import gradio as gr
3
- from openai import OpenAI
4
  from dotenv import load_dotenv
5
 
6
- # Load environment variables from .env file
7
  load_dotenv()
 
8
 
9
- # Retrieve API key from environment variables
10
- api_key = os.getenv("OPENAI_API_KEY")
 
11
 
12
- # Ensure the API key is available
13
- if not api_key:
14
- raise ValueError("API key not found. Please set OPENAI_API_KEY in your .env file.")
 
 
 
 
 
 
 
 
 
15
 
16
- # Initialize OpenAI client
17
- client = OpenAI(api_key=api_key)
18
-
19
- # Define chatbot function
20
- def chatbot(prompt):
21
- try:
22
- response = client.chat.completions.create(
23
- model="gpt-4o",
24
- messages=[
25
- {"role": "system", "content": "You are a helpful assistant."},
26
- {"role": "user", "content": prompt},
27
- ]
28
- )
29
- return response.choices[0].message.content
30
- except Exception as e:
31
- return f"Error: {str(e)}"
32
-
33
- # Create Gradio interface
34
  iface = gr.Interface(
35
- fn=chatbot,
36
- inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
37
  outputs="text",
38
- title="AI Chatbot",
39
- description="Chat with AI using OpenAI GPT-4o",
40
- theme="compact",
41
  )
42
 
43
- # Launch Gradio app
44
  if __name__ == "__main__":
45
  iface.launch()
 
1
  import os
2
  import gradio as gr
3
+ import requests
4
  from dotenv import load_dotenv
5
 
6
+ # Load API key from .env file
7
  load_dotenv()
8
+ github_api_key = os.getenv("GITHUB_API_KEY")
9
 
10
+ # Ensure API key is available
11
+ if not github_api_key:
12
+ raise ValueError("GitHub API key is missing. Check your .env file.")
13
 
14
+ # Define a function to fetch GitHub user details
15
+ def get_github_user(username):
16
+ url = f"https://api.github.com/users/{username}"
17
+ headers = {"Authorization": f"token {github_api_key}"}
18
+
19
+ response = requests.get(url, headers=headers)
20
+
21
+ if response.status_code == 200:
22
+ data = response.json()
23
+ return f"User: {data['login']}\nName: {data.get('name', 'N/A')}\nPublic Repos: {data['public_repos']}\nFollowers: {data['followers']}"
24
+ else:
25
+ return f"Error: {response.status_code} - {response.json().get('message', 'Unknown error')}"
26
 
27
+ # Create Gradio UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  iface = gr.Interface(
29
+ fn=get_github_user,
30
+ inputs=gr.Textbox(label="GitHub Username", placeholder="Enter GitHub username"),
31
  outputs="text",
32
+ title="GitHub User Info Fetcher",
33
+ description="Enter a GitHub username to fetch profile details.",
 
34
  )
35
 
36
+ # Launch the app
37
  if __name__ == "__main__":
38
  iface.launch()